package subject_set.top101;

/**
 * @author haomin
 * @date 2022/11/03 18:59
 **/
public class BM40 {
    public class Solution {
        public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
            return buildTree(pre, vin, 0, vin.length - 1);
        }
        int ind = 0;
        private TreeNode buildTree(int[] pre, int[] vin, int l, int r){
            if(l > r) return null;
            if(ind > vin.length) return null;

            TreeNode cur = new TreeNode(pre[ind]);
            ind++;
            int post = findInVin(vin, cur.val);
            cur.left = buildTree(pre, vin, l, post-1);
            cur.right = buildTree(pre, vin, post+1, r);
            return cur;
        }
        private int findInVin(int[] vin, int k){
            for(int i = 0; i < vin.length; ++i)
                if(vin[i] == k) return i;
            return -1;
        }
    }
}